package com.guslegend.controller;


import com.guslegend.enums.BizCodeEnum;
import com.guslegend.request.UserLoginRequest;
import com.guslegend.request.UserRegisterRequest;
import com.guslegend.service.FileService;
import com.guslegend.service.UserService;
import com.guslegend.util.JsonData;
import com.guslegend.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guslegend
 * @since 2025-05-24
 */
@Api(tags = "用户文档")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private FileService fileService;


    @ApiOperation("用户头像上传")
    @PostMapping("upload_headImg")
    /**
     * @requestPart注解 接收文件以及其他更为复杂的数据类型
     */
    public JsonData uploadUserHeadImg(@ApiParam(value = "用户头像") @RequestPart("file") MultipartFile file) {
        String imgUrl = fileService.uploadUserHeadImg(file);
        return imgUrl == "" ? JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL) : JsonData.buildSuccess(imgUrl);
    }

    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("register")
    public JsonData register(@ApiParam(value = "用户注册对象") @RequestBody UserRegisterRequest userRegisterRequest) {
        return userService.register(userRegisterRequest);
    }

    /**
     * 用户密码登录
     * @param userLoginRequest
     * @return
     */
    @ApiOperation("用户密码登录")
    @PostMapping("login_by_password")
    public JsonData loginByPassword(@ApiParam(value = "用户密码登录对象") @RequestBody UserLoginRequest userLoginRequest) {
        return userService.loginByPassword(userLoginRequest);
    }

    /**
     * 用户密码登录
     * @param userLoginRequest
     * @return
     */
    @ApiOperation("用户验证码登录")
    @PostMapping("login_by_code")
    public JsonData loginByCode(@ApiParam(value = "用户验证码登录对象") @RequestBody UserLoginRequest userLoginRequest) {
        return userService.loginByCode(userLoginRequest);
    }

    @ApiOperation("用户详情")
    @GetMapping("detail")
    public JsonData detail(){
        UserVO userVO=userService.findUserDetails();
        return JsonData.buildSuccess();
    }
}


